package com.companyName.sample.service.impl.sys;

import com.companyName.sample.bean.dto.sys.Tree;
import com.companyName.sample.common.exception.BusinessException;
import com.companyName.sample.common.util.CommUtil;
import com.companyName.sample.common.util.PageInfo;
import com.companyName.sample.data.entity.RolePo;
import com.companyName.sample.service.business.RoleBiz;
import com.google.common.collect.Lists;
import com.companyName.sample.data.mapper.RoleMapper;
import com.companyName.sample.data.mapper.RoleResourceMapper;
import com.companyName.sample.data.mapper.UserRoleMapper;
import com.companyName.sample.bean.dto.sys.RoleDto;
import com.companyName.sample.bean.dto.sys.RoleResourceDto;
import com.companyName.sample.api.service.sys.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    private static Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    private RoleBiz roleBiz;

    @Override
    public void findDataGrid(PageInfo pageInfo) {
        roleBiz.findDataGrid(pageInfo);
    }

    @Override
    public List<Tree> findTree() {
        return roleBiz.findTree();
    }

    @Override
    public void addRole(RoleDto roleDto) {
        RolePo rolePo = new RolePo();
        CommUtil.copyProperties(roleDto, rolePo);
        int insert = roleBiz.addRole(rolePo);
        if (insert != 1) {
            logger.warn("插入失败，参数：{}", roleDto.toString());
            throw new BusinessException("插入失败");
        }
    }

    @Override
    public void deleteRoleById(Long id) {
        int update = roleBiz.deleteRoleById(id);
        if (update != 1) {
            logger.warn("删除失败，id：{}", id);
            throw new BusinessException("删除失败");
        }
    }

    @Override
    public RoleDto findRoleById(Long id) {
        return roleBiz.findRoleById(id);
    }

    @Override
    public void updateRole(RoleDto roleDto) {
        RolePo rolePo = new RolePo();
        CommUtil.copyProperties(roleDto, rolePo);
        int update = roleBiz.updateRole(rolePo);
        if (update != 1) {
            logger.warn("更新失败，参数：{}", roleDto.toString());
            throw new BusinessException("更新失败");
        }
    }

    @Override
    public List<Long> findResourceIdListByRoleId(Long id) {
        return roleBiz.findResourceIdListByRoleId(id);
    }

    @Override
    public void updateRoleResource(Long id, String resourceIds) {
        roleBiz.updateRoleResource(id,resourceIds );
    }

    @Override
    public List<Long> findRoleIdListByUserId(Long userId) {
        return roleBiz.findRoleIdListByUserId(userId);
    }

    @Override
    public List<Map<Long, String>> findRoleResourceListByRoleId(Long roleId) {
        return roleBiz.findRoleResourceListByRoleId(roleId);
    }

}
